import os

import requests
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import macro


class Api:
    def __init__(self, token):
        self.headers = None
        self.log = Logger()
        self.comm_sh = CommonSH()
        self.token = token
        self.ip = f"https://{macro.DATAKIT_HOST}:{macro.DATAKIT_PORT}"

    def get_system_settings(self):
        api = "/system/setting"
        url = self.ip + api
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def update_system_settings(self, data):
        api = "/system/setting"
        url = self.ip + api
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        response = requests.put(url=url, json=data, headers=self.headers, verify=False)
        return response

    def get_index_instance_router(self):
        api = "/getIndexInstanceRouters"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_get_info(self):
        api = "/getInfo"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_get_routers(self):
        api = "/getRouters"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 停止插件
    def post_system_plugin_stop(self, plugin_name: str) -> str:
        """
        停止插件
        :param plugin_name: 插件名称
        :return: 响应状态码
        """
        api = "/system/plugins/stop/"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api + plugin_name
        response = requests.post(url=url, headers=self.headers, verify=False)
        return response

    # 启动插件
    def post_start_plugin(self, plugin_name: str) -> str:
        """
        启动插件
        :param plugin_name: 插件名称
        :return: 响应状态码
        """
        api = "/system/plugins/start/"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api + plugin_name
        response = requests.post(url=url, headers=self.headers, verify=False)
        return response

    def get_system_log(self) -> str:
        """
        获取系统日志配置信息
        :return: 响应状态码和日志配置信息
        """
        api = "/system/log"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def post_system_log(self, data):
        """
        设置系统日志配置
        :return: 响应状态码
        """
        api = "/system/log"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    def get_system_log_files(self):
        """
        获取日志文件列表
        :return: 响应状态码和日志文件列表
        """
        api = "/system/log/files"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_system_logs_print(self):
        """
        获取日志打印等级列表
        :return: 响应状态码和日志打印等级列表
        """
        api = "/system/log/print"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_system_log_download(self, filename:str) -> str:
        """
        下载系统日志
        :param filename: 日志文件名
        :return: 响应状态码
        """
        api = "/system/log/download?filenames="
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api + filename
        response = requests.head(url, headers=self.headers, verify=False)
        return response

    # 获取菜单列表
    def get_menu_list(self, data=None):
        api = "/system/menu/list"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, data=data, headers=self.headers, verify=False)
        return response

    # 获取用户菜单树列表
    def get_role_menu_tree_select(self, data):
        api = f"/system/menu/roleMenuTreeselect/{data}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 获取菜单树列表
    def get_menu_tree_list(self, data=None):
        api = "/system/menu/treeselect"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, data=data, headers=self.headers, verify=False)
        return response

    # 使用 menuId 获取菜单信息
    def get_menu_info(self, data):
        api = f"/system/menu/{data}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 新增菜单
    def add_menu(self, data):
        api = "/system/menu"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    # 修改菜单
    def update_menu(self, data):
        api = "/system/menu"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.put(url=url, json=data, headers=self.headers, verify=False)
        return response

    # 删除菜单
    def delete_menu(self, data):
        api = f"/system/menu/{data}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.delete(url=url, headers=self.headers, verify=False)
        return response

    # 获取系统插件列表
    def get_plugin_info_list(self):
        api = "/system/plugins/listContent"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 查询插件,data可使用pageNum，pageSize，pluginId，pluginProvider，pluginStatus，pluginType
    def query_plugin_list(self, data=None):
        api = f"/system/plugins/list?{data}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def count_plugin(self):
        api = "/system/plugins/count"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 通过pluginId获取插件信息
    def get_plugin_info(self, plugin_id):
        api = f"/system/plugins/get/{plugin_id}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, headers=self.headers, verify=False)
        return response

    # 修改插件配置
    def save_plugin_config_data(self, data):
        api = "/system/plugins/pluginConfigData"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    # 安装插件，data为插件jar包路径
    def install_plugin(self, jar_file):
        api = "/system/plugins/install"
        self.headers = {
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        with open(jar_file, 'rb') as file:
            files = {'file': file}
            response = requests.post(url=url, files=files, headers=self.headers, verify=False)
        return response

    # 卸载插件
    def post_uninstall_plugin(self, data):
        api = f"/system/plugins/uninstall/{data}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, headers=self.headers, verify=False)
        return response

    # 查询插件扩展列表
    def get_query_plugin_extension_list(self, data=None):
        api = f"/system/plugins/extensions/list?{data}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 获取所有角色列表
    def get_role_list(self):
        api = "/system/role/optionselect"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 通过role_id获取角色信息
    def get_role_by_id(self, role_id):
        api = f"/system/role/{role_id}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 使用pageNum、pageSize、roleName、status查询角色
    def query_role_list(self, data=None):
        api = f"/system/role/list?{data}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def add_role(self, data):
        api = "/system/role"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    # role_ids为要删除的角色id，数据类型为数组，支持批量删除
    def delete_role(self, role_ids):
        array_str = '/'.join(map(str, role_ids))
        api = f"/system/role/{array_str}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.delete(url=url, headers=self.headers, verify=False)
        return response

    # 更新角色信息
    def update_role(self, data):
        api = "/system/role"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.put(url=url, json=data, headers=self.headers, verify=False)
        return response

    # 更新角色状态
    def update_role_status(self, data):
        api = "/system/role/changeStatus"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.put(url=url, json=data, headers=self.headers, verify=False)
        return response

    def check_upload_path(self, path):
        api = f"/system/setting/checkUploadPath?path={path}"
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 条件查询页面中的任务列表,data为“参数=值”格式
    def query_task_page(self, data=None):
        api = f'/sys/task/list?{data}'
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 条件查询任务列表,data为“参数=值”格式
    def query_task_list(self, data=None):
        api = f'/sys/task/list/all?{data}'
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    # 查询用户页面,data为“参数=值”格式
    def query_user_page(self, data):
        api = f'/system/user/list?pageNum=1&pageSize=10&{data}'
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_user_by_id(self, user_id):
        api = f'/system/user/{user_id}'
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def add_user(self, data):
        api = '/system/user'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    def delete_user(self, user_id):
        array_str = '/'.join(map(str, user_id))
        api = f'/system/user/{array_str}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.delete(url=url, headers=self.headers, verify=False)
        return response

    def get_white_list_all(self):
        api = f'/sys/whiteList/list/all'
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_white_list(self, whilelist_name):
        """
        查询单个安全中心-访问白名单信息
        :param whilelist_name:白名单名称
        :return:执行get请求返回信息
        """
        api = f'/sys/whiteList/list?pageNum=1&pageSize=10&title={whilelist_name}'
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def delete_white_list(self, whilelist_id):
        """
        删除单个安全中心-访问白名单信息
        :param whilelist_id:白名单id
        :return:执行delete请求返回信息
        """
        api = f'/sys/whiteList/{whilelist_id}'
        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.delete(url=url, headers=self.headers, verify=False)
        return response

    def add_whitelist(self, data):
        """
        添加安全中心-访问白名单信息
        :param data:白名单信息json格式，包含白名单名称和ip地址{"title":"hahahahfgds","ipList":"168.1.1.1"}
        :return:执行post请求信息
        """
        api = '/sys/whiteList'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    def update_white_list(self, data) -> str:
        """
        更新安全中心-访问白名单中单个白名单信息
        :param data:待更新白名单信息json格式，包含白名单id,名称和ip地址{"id":14,"title":"ss","ipList":"12.13.15.168"}
        :return:执行put请求返回信息
        """
        api = f'/sys/whiteList'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.put(url=url, json=data, headers=self.headers, verify=False)
        return response

    def post_host_ping(self, data):
        """
        导入服务器前，连通性测试
        :param data:json格式服务器信息
        :return:
        """
        api = '/host/ping'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    def post_host(self, data):
        """
        服务器管理，新增服务器
        :param data:json格式服务器信息
        :return:
        """
        api = '/host'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    def get_host_page(self, name: str = '', tag_ids: str = '',
                      system_os: str = '', page_num: int = 1,
                      page_size: int = 10) -> str:
        """
        服务器管理界面，查询服务器
        :param name: 按服务器ip查询
        :param tag_ids: 按标签查询
        :param system_os: 按操作系统查询
        :param page_num: 查询第几页的服务器
        :param page_size: 每页最大服务器数量，为10的倍数
        :return: 返回状态码和查询到的服务器信息
        """
        api = f'/host/page?name={name}&tagIds={tag_ids}&os={system_os}&pageNum={page_num}&pageSize={page_size}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_clusterTask_host_list(self, system_os: str = '',
                                                   os_version: str = '', cpu_arch: str = '') -> str:
        """
        获取已导入服务器的列表
        :param system_os: 操作系统
        :param os_version: 操作系统版本
        :param cpu_arch: cpu架构
        :return: 符合条件的已导入的服务器的列表
        """
        api = f'/plugins/base-ops/clusterTask/host/list?os={system_os}&osVersion={os_version}&cpuArch={cpu_arch}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def post_hostUser(self, data):
        """
        服务器新增用户
        :param data: json格式用户信息
        :return:
        """
        api = '/hostUser'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    def delete_host(self, host_id):
        """
        datakit删除服务器
        :param host_id: host_id
        :return:
        """
        api = f'/host/{host_id}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.delete(url=url, headers=self.headers, verify=False)
        return response